﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class updateData : System.Web.UI.Page
{
    bussiness bus;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            bus = new bussiness();
            ddlTypeCardName.DataTextField = "typeCardName";
            ddlTypeCardName.DataValueField = "typeEcardId";
            ddlTypeCardName.DataSource = bus.getAllTypeEcard();
            ddlTypeCardName.DataBind();
            load_data();
        }

    }
    public void load_data()
    {
        bus = new bussiness();
        
        GridView1.DataSource = bus.getViewDisplayEcard();
        GridView1.DataBind();
        
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        bus = new bussiness();
        bool flag = true;
        string urlImage = txtUrlImage.Text;
        string urlFlash = txtUrlFlash.Text;
        string nameEcard = txtNameEcard.Text;
        int typeCardId = Convert.ToInt32(ddlTypeCardName.SelectedValue);
        if (urlImage == "" || urlFlash == "" || nameEcard == "")
        {
            flag = false;
            lblInfo.Text = "fields blank ";
        }
        else
        {
            List<Ecard> lstEcard = bus.getAllEcard();
            foreach(Ecard ecard in lstEcard)
            {
                if(ecard.UrlFlash == urlFlash)
                {
                    flag = false;
                    lblInfo.Text = "urlFlash is existed ";
                    break;
                }
            }
        }
        if(flag)
        {
            lblInfo.Text = "";
            Ecard ecard = new Ecard();
            ecard.UrlImage = urlImage;
            ecard.UrlFlash = urlFlash;
            ecard.EcardName = nameEcard;
            ecard.TypeId = typeCardId;
            bus.insertEcard(ecard);
            lblInfo.Text = "insert Ecard Successful";
            txtUrlImage.Text = "";
            txtUrlFlash.Text = "";
            txtNameEcard.Text = "";
        }
    }
    protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        GridView1.PageIndex = e.NewPageIndex;
        load_data();
    }
    protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
    {
        GridView1.EditIndex = e.NewEditIndex;
       
        load_data();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        load_data();
    }
    protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        bus = new bussiness();
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        List<SentEcard> lstSentEcard = bus.getAllSentEcard();
        bool flag = true;
        foreach (SentEcard se in lstSentEcard)
        { 
            if(se.EcardId == id)
            {
                flag = false;
                break;
            }
        }
        List<LogSentMail> lstLogSentMail = bus.getLogSentMail();
        foreach (LogSentMail lsm in lstLogSentMail)
        { 

            if(lsm.EcardId == id )
            {
                flag = false;
                break;
            }
        }
        if (flag)
        {
            bus.deleteEcard(id);
            load_data();
            lblError.Text = "Delete Successful";
        }
        else 
        {
            lblError.Text = "Don't delete because Ecard using!";
        }
    }
    protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        if (e.Row.RowType == DataControlRowType.DataRow)
        {
           
            if ((e.Row.RowState & DataControlRowState.Edit) > 0)
            {
                DropDownList ddList = (DropDownList)e.Row.FindControl("ddlTypeEcard");
                //bind dropdownlist
                List<TypeEcard> lstTypeEcard = bus.getAllTypeEcard();
                ddList.DataTextField = "typeCardName";
                ddList.DataValueField = "typeEcardId";
                ddList.DataSource = lstTypeEcard;
                ddList.DataBind();
                //DataRowView dr = e.Row.DataItem as DataRowView;
                //ddList.SelectedItem.Text = dr["typeCardName"].ToString();
               
            }
        }
    }
    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        bus = new bussiness();
        int id = int.Parse(GridView1.DataKeys[e.RowIndex].Value.ToString());
        List<Ecard> lstEcard = bus.getAllEcard();
        bool flag = true;
        //string nameTypeEcard = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
        string urlFlash = (GridView1.Rows[e.RowIndex].Cells[1].Controls[0] as TextBox).Text;
        string ecardName = (GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox).Text;

        int typeId = Convert.ToInt32((GridView1.Rows[e.RowIndex].Cells[4].Controls[1] as DropDownList).SelectedValue);
        if (urlFlash == "" || ecardName == "")
        {
            flag = false;
            lblError.Text = "must is not null";
            load_data();
        }
        //else
        //{
        //    foreach (Ecard ec in lstEcard)
        //    {
        //        if (ec.UrlFlash == ec.UrlFlash)
        //        {
        //            flag = false;
        //            lblError.Text = "must is not null";
        //            break;
        //        }
        //    }
        //}
        if (flag)
        {
            bus.updateEcard(id, urlFlash, ecardName, typeId);
            GridView1.EditIndex = -1;
            load_data();
            lblError.Text = "";
        }
    }
}